﻿
package com.ostrich.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.HtmlUtil;
import com.base.util.SessionUtils;
import com.base.web.BaseAction;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.bean.TostHrPostinfo;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;
import com.ostrich.manpower.service.impl.TostHrDepartinfoService;
import com.ostrich.manpower.service.impl.TostHrPostinfoService;
import com.ostrich.project.bean.TostPjCostitem;
import com.ostrich.project.bean.TostPjGeneraltable;
import com.ostrich.project.service.impl.TostPjGeneraltableService;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostPjGeneraltable")
public class TostPjGeneraltableController extends BaseAction
{
	private final static Logger log= Logger.getLogger(TostPjGeneraltableController.class);

	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjGeneraltableService<TostPjGeneraltable> tostPjGeneraltableService; 
	@Autowired(required=false)
	private TostHrBaseinfoService<TostHrBaseinfo> tosthrbaseinfoservice;
	@Autowired(required=false)
	private TostHrPostinfoService<TostHrPostinfo> tosthrpostinfoservice;
	@Autowired(required=false)
	private TostPjGeneraltableService<TostPjGeneraltable> tostpjgeneraltableservice;
	/**
	 * ilook 首页
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView  list(TostPjGeneraltable tostPjGeneraltable,HttpServletRequest request) throws Exception{
		
		Map<String,Object>  context = getRootMap();
		//判断页面是否直接跳转add的form表单的展示界面
		Integer count = tostPjGeneraltableService.queryByCount(tostPjGeneraltable);
		// 判断页面是否直接跳转add的form表单的展示界面
		String flag = (count<=0)? "add": "list";
		
		Map<String,Object> mp = FormulaUtil.formulaById(10,tostPjGeneraltable.getPjId(),tostPjGeneraltable.getPctId());
		request.getSession().setAttribute("pjgl",tostPjGeneraltable);
		
		//List<TostPjGeneraltable> dataList = tostPjGeneraltableService.queryByList(tostPjGeneraltable);
		//设置页面数据
		context.put("flag", flag);
		context.put("pjgen", tostPjGeneraltable);
		context.put("mp",mp);
		return forword("project/tabTostPjGeneraltable",context);
	}
	
	/**
	 * json 列表页面
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList")
	public void  dataList(TostPjGeneraltable tostPjGeneralinfo, HttpServletResponse response,HttpServletRequest request) throws Exception{
		
		TostPjGeneraltable tostPjGeneraltable=(TostPjGeneraltable) request.getSession().getAttribute("pjgl");
		
		tostPjGeneraltable.setPage(tostPjGeneralinfo.getPage());
		tostPjGeneraltable.setRows(tostPjGeneralinfo.getRows());
		
		List<TostPjGeneraltable> dataList = tostPjGeneraltableService.queryByList(tostPjGeneraltable);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",tostPjGeneraltable.getPager().getRowCount());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}


	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	public void save(TostPjGeneraltable bean,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		String name=SessionUtils.getUser(request).getNickName();
		TostPjGeneraltable tostPjGeneraltable=(TostPjGeneraltable)request.getSession().getAttribute("pjgl");
		
		bean.setPjId(tostPjGeneraltable.getPjId());
		bean.setPctId(tostPjGeneraltable.getPctId());
		
		//判断是否是手动输入的人员名称
		TostHrBaseinfo baseinfo = new TostHrBaseinfo();
		baseinfo.setHrName(bean.getHrName());
		List<TostHrBaseinfo> bsls =  tosthrbaseinfoservice.queryByList(baseinfo);
		if(bsls==null || bsls.size()<=0){
			baseinfo.setHrDepart(Integer.toString(0));
			baseinfo.setHrJobs(Integer.toString(0));
			baseinfo.setHrJobsName("默认岗位");
			bean.setHrId(tosthrbaseinfoservice.addAndReturn(baseinfo).getId());
		}
		
		tostPjGeneraltableService.addGeneral(bean,name);
		sendSuccessMessage(response, "保存成功~");
	}
	
	@RequestMapping("/hrIdList")
	public void hrIdList(HttpServletResponse response){
		List<TostHrBaseinfo> data=tosthrbaseinfoservice.queryAll();
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",(data!=null)?data.size():0);
		jsonMap.put("data", data);
		HtmlUtil.writerJson(response, jsonMap);
	}
	@RequestMapping("/hpoidList")
	public void hpoidList(HttpServletResponse response) throws Exception{
		
		List<TostHrPostinfo> data=tosthrpostinfoservice.queryByAll();
		
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("total",(data!=null)?data.size():0);
		jsonMap.put("data", data);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/postypenocash")
	public void postypenocash(Integer hpoId,HttpServletResponse response) throws Exception{
		
		TostHrPostinfo postinfo=tosthrpostinfoservice.queryById(hpoId);
		//获取为合计的非限制的金额数量 110
		Map<String, Object> map = FormulaUtil.formulaById(12,postinfo.getHpoType(), 110);
		Float cash = (map != null && map.get("cash")!=null) ? (Float) map.get("cash") : new Float(0);
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("cash", cash);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response) throws Exception{
		Map<String,Object>  context = getRootMap();
		TostPjGeneraltable bean  = tostPjGeneraltableService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletResponse response) throws Exception{
		tostPjGeneraltableService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/getPjUntotal")
	public void getPjUntotal(HttpServletResponse response) {
		
		List<TostPjGeneraltable> dataList = tostpjgeneraltableservice.queryGen();
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", (dataList.size() == 0) ? 0 : dataList.size());
		jsonMap.put("data", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
}
